package nc.ui.arap.view;

import java.util.HashMap;
import java.util.Map;

import nc.ui.arap.action.ActionHandler;
import nc.ui.pub.ButtonObject;
import nc.vo.pub.BusinessException;

public abstract class Controler<T> {
	
	private Map<String, ActionHandler<T>> actions = new HashMap<String, ActionHandler<T>>();
	
	public Controler() {
//		ResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
//		try {
//			String classPackage = this.getClass().getPackage().getName().replace("view", "action.");
//			String classPattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + classPackage.replaceAll("\\.", "/") + "*";
//			Resource[] resources = patternResolver.getResources(classPattern);
//			for (Resource resource : resources) {
//				Class clazz = this.getClass().getClassLoader().loadClass(classPackage + resource.getFilename().replace(".class", ""));
//				if (clazz.isAnnotationPresent(Action.class) && ActionHandler.class.isAssignableFrom(clazz)) {
//					actions.put(((Action) clazz.getAnnotation(Action.class)).code(), (ActionHandler) clazz.newInstance());
//				}
//			}
//		} catch (IOException e) {
//			e.printStackTrace();
//		} catch (ClassNotFoundException e) {
//			e.printStackTrace();
//		} catch (InstantiationException e) {
//			e.printStackTrace();
//		} catch (IllegalAccessException e) {
//			e.printStackTrace();
//		}
		actions = getActionMap();
	}
	
	public void handle(ButtonObject buttonObject, ViewContext<T> context) throws BusinessException {
		context.lockUI();
		new ActionSwingWorker<T>(this, buttonObject, context).start();
	}
	
	public abstract Map<String, ActionHandler<T>> getActionMap();

}
